package _15_generics._15_10.exercise;

import typeinfo.pets.Cat;
import typeinfo.pets.Mouse;
import typeinfo.pets.Pet;
import typeinfo.pets.Rodent;

import java.util.Calendar;

/**
 * Create a generic class Generic1<T> with a single
 * method that takes an argument of type T. Create
 * a second generic class Generic2<T> with a single
 * method that returns an argument of type T. Write
 * a generic method with a contravariant argument of
 * the first generic class that call its method.
 * Write a second generic method with a covariant
 * argument of second generic class that calls
 * its method。 Test using the typeinfo.pets library.
 */
public class E28_GenericReadAndWrite {
    static <T> void f1(Generic1<? super T> obj, T item){
        obj.set(item);
    }
    static <T> T f2(Generic2<? extends T> obj){
        return obj.get();
    }

    public static void main(String[] args) {
        Generic1<Rodent> g1 = new Generic1<>();
        f1(g1,new Mouse());
//        f1(g1,new Cat());
        Generic2<Pet> g2 = new Generic2<>();
        Pet pet = g2.get();
        Generic2<Cat> g3 = new Generic2<>();
        Cat cat = g3.get();
        pet = f2(g3);
    }
}

class Generic1<T> {
    public void set(T arg){}
}

class Generic2<T> {
    T get(){
        return null;
    }
}
